package ge.info.pages;

import ge.info.domain.City;
import ge.info.domain.Street;
import ge.info.domain.transport.Transport;
import ge.info.domain.transport.TransportType;
import ge.ktapestry.pages.BasePageExt;
import ge.ktools.StringUtils;
import java.util.ArrayList;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;

import java.util.List;

public abstract class IncityTransport extends BasePageExt implements PageBeginRenderListener
{
    public static int SERVICE_ID = 26;

    @Persist
    public abstract TransportType getTransportType();

    public abstract void setTransportType(TransportType transportType);

    //@Persist
    public abstract List<Transport> getRoutePartsDown();

    public abstract void setRoutePartsDown(List<Transport> routeParts);

    //@Persist
    public abstract List<Transport> getRoutePartsUp();

    public abstract void setRoutePartsUp(List<Transport> routeParts);

    @Persist
    public abstract Transport getTransport();

    public abstract void setTransport(Transport transport);

    //  @Persist
    public abstract List<Transport> getTransports();

    public abstract void setTransports(List<Transport> transports);

    public abstract Transport getLoopTransport();

    public abstract void setLoopTransport(Transport transport);

    @Persist
    public abstract Street getStreet();

    public abstract void setStreet(Street street);

    @Persist
    public abstract City getCity();

    public abstract void setCity(City city);

    @Persist
    public abstract String getTrno();

    public abstract void setTrno(String trno);

    //   @Persist
    public abstract List<Street> getStreetsFound();

    public abstract void setStreetsFound(List<Street> streetsFound);

    public abstract Street getLoopStreet();

    public abstract void setLoopStreet(Street street);
     @Persist
    public abstract String getRouteName();
    public abstract void  setRouteName(String s);

    public void pageBeginRender(PageEvent event)
    {
        if (!event.getRequestCycle().isRewinding())
        {
            if (getTransportType() == null)
            {
                setTransportType((TransportType) getComboes().getCombo("IncityTransportTypes").getOption(0));
            }
            if (getTransport() == null)
            {
                Transport transport = new Transport();
                transport.setTransportType(getTransportType());
                setTransport(transport);
            }

            if (getCity() == null)
            {
                City city = new City();
                city.setCityID(2208);
                setCity(city);
            }
            if (getStreet() == null)
            {
                setStreet(new Street());
            }
        }

    }
    public void updateTransportType(IRequestCycle cycle)
    {
        getTransport().setTransportType(getTransportType());
    }
    public void searchTransportByNo(IRequestCycle cycle)
    {
        if (getDelegate().getHasErrors())
        {
            return;
        }
        setTransports(getDbExecutor().searchTransportByNo(getTransport()));
        getTransport().setInPlace(null);
        getTransport().setOutPlace(null);
        setStreet(new Street());
    
     
    }

    public void searchTransportByRoute(IRequestCycle cycle)
    {
        if (getDelegate().getHasErrors())
        {
            return;
        }

        setTransports(getDbExecutor().searchTransportByRoute(getTransport()));
        setStreet(new Street());
        
    }

    public void showRoute(IRequestCycle cycle, Transport transport)
    {
        setTrno(transport.getTransportNo());
         
        List<Transport> routePartsDown = new ArrayList<Transport>();
        List<Transport> routePartsUp = new ArrayList<Transport>();
        List<Transport> routeParts = getDbExecutor().getRouteParts(transport);
        if (routeParts != null)
        {
           
            for (Transport tr : routeParts)
            {
                if (tr.getDirection() == 0 || tr.getDirection() == 1)
                {
                    routePartsDown.add(tr);
                }
                else
                {
                    routePartsUp.add(tr);
                }
            }
            Transport searchedTransport = getTransport();
            if (searchedTransport.getOutPlace() != null && searchedTransport.getInPlace() != null)
            {

                for (Transport tr : routeParts)
                {

                    if (StringUtils.containsAllStrings(tr.getOutPlace(), StringUtils.splitString(searchedTransport.getOutPlace())) ||
                            StringUtils.containsAllStrings(tr.getOutPlace(), StringUtils.splitString(searchedTransport.getInPlace())))
                    {
                        tr.setSearchedFor(true);
                    }
                    else
                    {
                        tr.setSearchedFor(false);
                    }
                }
            }
        }

       
        
        setRoutePartsDown(routePartsDown);
        setRoutePartsUp(routePartsUp);
    }

    public void searchStreet(IRequestCycle cycle)
    {

        Street street = getStreet();
        street.setCityID(getCity().getCityID() == 0 ? null : getCity().getCityID());
        setStreetsFound(getDbExecutor().searchStreet(street));
        setTransport(null);

    }

    public void sendSMS(IRequestCycle cycle, Street street)
    {

        StringBuilder sb = new StringBuilder();

        sb.append(street.getDisplayName()).append(" ");
        if (street.getStreetIndex() != null)
        {
            sb.append(street.getStreetIndex()).append(" ");
        }
        if (street.getStreetDistrict() != null)
        {
            sb.append(street.getStreetDistrict()).append(" ");
        }
        if (street.getLocation() != null)
        {
            sb.append(street.getLocation());
        }
        getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
    }
    public void sendTransportSMS(IRequestCycle cycle, Transport trans)
    {

        StringBuilder sb = new StringBuilder();
        sb.append("Nomeri ");
        sb.append(trans.getTransportNo()).append(" ");
        sb.append(trans.getOutPlace()).append(" ");
        sb.append(trans.getInPlace()).append(" ");
        if (trans.getDirection() != 1)
        {
            sb.append("tsriuli");
        }
        getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
    }
    public void sendRoutePartsDown(IRequestCycle cycle)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("Nomeri ");
        sb.append(getRouteName()).append(" ");
        for (Transport tr: getRoutePartsDown())
        {
            if (tr.isSearchedFor())
            {
                sb.append(tr.getOutPlace()).append(" ");
            }
        }
        getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
        setRoutePartsUp(null);
        setRoutePartsDown(null);

    }
    public void sendRoutePartsUP(IRequestCycle cycle)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("Nomeri ");
        sb.append(getRouteName()).append(" ");
        for (Transport tr: getRoutePartsUp())
        {
            if (tr.isSearchedFor())
            {
                sb.append(tr.getOutPlace()).append(" ");
            }
        }
        getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
        setRoutePartsUp(null);
        setRoutePartsDown(null);
    }
}

